package TechnicalServices;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import DomainPackage.*;
import Foundation.*;
public class DataConnector {

    private static DataConnector thisDataConnector;
    private Statement stmt;
    private Connection con;
    private ResultSet rs;
            
    
    private String POSTGRESQL_PORT="5432";
    private String POSTGRESQL_IP="localhost";
    private String POSTGRESQL_DATABASE_NAME="ayo";
    private String POSTGRESQL_USERNAME="postgres";
    private String POSTGRESQL_PASSWORD="";
    
    
    private DataConnector(){
            startConnection();
    }
    
    public static DataConnector getInstance(){
            if( thisDataConnector == null)
                    thisDataConnector = new DataConnector();
            return thisDataConnector;
                    
    }
    
    private boolean startConnection(){
            try{
                    Class.forName("org.postgresql.Driver");
                    con = DriverManager.getConnection(("jdbc:postgresql://"+POSTGRESQL_IP+":"+POSTGRESQL_PORT+"/"+POSTGRESQL_DATABASE_NAME), POSTGRESQL_USERNAME, POSTGRESQL_PASSWORD);
                    stmt = con.createStatement();
                    
                    return true;
            }
            catch(Exception e){
                    e.printStackTrace();
                    return false;
            }
    }
    
    public ResultSet query(String query){
            try{
                    rs = stmt.executeQuery(query);
                    return rs;
            }
            catch(Exception e){
                    e.printStackTrace();
                    return null;
            }
    }
    
    public Boolean update(String query){
            try{
                    stmt.executeUpdate(query);
                    System.out.println("query ok");
                    return true;
            }
            catch(Exception e){
                    e.printStackTrace();
                    return false;
            }
    }

    public void printLastQueryResult(){
            try {
                    if (rs != null){
                            while(rs.next()){
                                    ResultSetMetaData rsMetaData = rs.getMetaData();
                                    int numberOfColumns = rsMetaData.getColumnCount()+1;
                                    for (int i=1;i<numberOfColumns;i++){
                                            System.out.println("column: " + rsMetaData.getColumnName(i) + " value: " + rs.getString(i));
                                    }
                            }
                    }
            }
            catch(Exception e){
                    e.printStackTrace();
            }
    }

    public int countRows(String tableName) throws SQLException {
            // select the number of rows in the table
            int rowCount = -1;
            try {
                    stmt = con.createStatement();
                    rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
                    // get the number of rows from the result set
                    rs.last();
                    rowCount = rs.getRow();
                    } 
            finally {
                    }
            return rowCount;
    }

    
    public static void main(String[] args){
    	
    	DataConnector d = new DataConnector();
    	d.startConnection();
	
    }
	
}
